<?php

namespace App\Http\Middleware;

use Closure;
// use Illuminate\Http\Request;
use App\Models\HomeModel;
use App\Http\Requests\MenuRequest;
use Illuminate\Http\Request;

class UserMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function __construct()
    {
        $action = \Route::currentRouteAction();
        list($controller, $method) = explode('@', $action);
        $controller = substr(strrchr($controller,'\\'),1);
        $class = str_replace("controller",'',strtolower($controller));
        // dd($class);
        $global = HomeModel::getGlobalVal();
        $nav = HomeModel::getNav();
        view()->share(['global'=> $global,'nav'=>$nav,'type_id'=>6,'class'=>$class,'method'=> $method]);
    }
    public function handle($request, Closure $next)
    {
        $url = $request->url();
        $arr = explode('/',$url);
        $action = end($arr);
        // dd($action);
        // if($action != 'login' && $action != 'code' && $action != 'userLogin'){
        if(empty(session('user'))){
            // return redirect()->action('Home\IndexController@message',['info'=>'请您登录','url'=>'/','res'=>0]);
            return redirect('/message')->with(['res'=>0,'msg'=>'请您登录','url'=>'/']);
        }else{
            // 判断用户是否完善资料
            if(!session('user')->status){
                // dd($this->uid);
                if($action!='setInf' && $action!='doSetInf'){
                    return redirect('user/setInf');
                }
            }
        }
        // }
        return $next($request);
    }



}
